package com.tt.handler;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import lombok.extern.log4j.Log4j2;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.context.annotation.Profile;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.Date;

@Component
@Log4j2
public class MyBatisPlusMetaObjectHandler implements MetaObjectHandler {

    // 插入时填充策略
    @Override

    public void insertFill(MetaObject metaObject) {

        log.info("开始插入");

        Date date = new Date();
        ZoneId zoneId = ZoneId.systemDefault();
        LocalDateTime localDateTime = date.toInstant().atZone(zoneId).toLocalDateTime();

        this.setFieldValByName("gmtModified", localDateTime, metaObject);
        this.setFieldValByName("gmtCreated", localDateTime, metaObject);


    }


    // 更新时填充策略
    @Override
    public void updateFill(MetaObject metaObject) {

        log.info("开始更新");

        Date date = new Date();
        ZoneId zoneId = ZoneId.systemDefault();
        LocalDateTime localDateTime = date.toInstant().atZone(zoneId).toLocalDateTime();

        this.setFieldValByName("gmtModified", localDateTime, metaObject);

    }
}
